<?php


namespace App\Http\Controllers;

use App\Models\CollectFile;
use App\Models\File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\UserFile;
use Illuminate\Support\Facades\DB;

class CollectFileController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    //收藏
    public function collect($id){
        $user = Auth::user();
        $collect_file = new CollectFile();
        $collect_file->user_file_id = $id;
        $collect_file->user_id = $user->id;
        if($collect_file->save()){
            return ['code'=>0, 'msg'=>'success'];
        }else{
            return ['code'=>1, 'msg'=>'save error'];
        }
    }
    //取消收藏
    public function cancel($id){
        $user = Auth::user();
        CollectFile::where('user_id', $user->id)->where('user_file_id', $id)->delete();
        return ['code'=>0, 'msg'=>'success'];
    }

    //获取收藏的文件
    public function  getFiles(Request $request){
        $user = Auth::user();
        $search_name = $request->input('name');
        $order = intval($request->input('order'));
        $key = intval($request->input('key'));
        $order_by = $this->_file_order($key, $order);
        $collect_files = DB::table('collect_files')
            ->where('collect_files.user_id', $user->id)
            ->where(function($query) use($search_name){
                if (!empty($search_name)) {
                    $query->where('user_files.name', 'like', '%' . $search_name . '%');
                }
            })
            ->where('user_files.status',  UserFile::STATUS_NORMAL)
            ->orderby($order_by['key'], $order_by['order'])
            ->join('user_files', 'user_files.id', '=', 'collect_files.user_file_id')
            ->join('files', 'files.id', '=', 'user_files.file_id')
            ->select('user_files.*')
            ->get();
        foreach($collect_files as $one) {
            $file = File::where('id', $one->file_id)->select('mime', 'hash')->first();
            if (in_array($file->mime, ['image/jpeg', 'image/png'])) {
                if (is_file(base_path() . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'thumb' . DIRECTORY_SEPARATOR . $file->hash . '.jpg')) {
                    $one->thumb = '/thumb/' . $file->hash . '.jpg';
                }
            }
        }
        return ['code'=>0, 'data'=>['files'=>$collect_files]];
    }
    /**
     * 文件排序方式
     */
    private function _file_order($key, $order) {
        $result = 'user_files.id';
        $sequence = 'asc';
        if (!empty($key)) {
            $sequence = 'desc';
            if($order == 1) {
                $sequence = 'asc';
            }
            switch ($key) {
                //名称
                case '1' :
                    $result = 'user_files.name';
                    break;
                //大小
                case '2' :
                    $result = 'files.real_size';
                    break;
                //时间
                case '3' :
                    $result = 'user_files.updated_at';
                    break;
            }
        }
        return array('key'=>$result, 'order'=>$sequence);
    }
}
